import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RoundInfo, RecmInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";

export interface SymmetricOption {
    redWinStep: number;
    redFailStep: number;
    blueWinStep: number;
    blueFailStep: number;
    blueStopLFailCount: number;
    baseMoney: number;
    betCount: number;
    stepMoney: number;
}

export class SymmetricMoneyStrategy implements IMoneyStrategy {
    option: SymmetricOption;
    recmV: EnumBjlResult = EnumBjlResult.U;
    currentMoney: number = 0;
    redWinStep: number = 1;
    redFailStep: number = 1;
    blueWinStep: number = 1;
    blueFailStep: number = 1;
    blueStopLFailCount: number = 3;
    recmMoneyArray: number[] = [];
    betCountArray: number[] = [];
    runing: boolean = true;
    betCount: number = 0;
    stepMoney: number = 10;
    /**
     *
     */
    constructor(name: string, option?: SymmetricOption) {
        this.strategyName = name;
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                redWinStep: 1,
                redFailStep: 1,
                blueWinStep: 1,
                blueFailStep: 1,
                blueStopLFailCount: 2,
                baseMoney: 10,
                stepMoney: 10,
                betCount: 0
            };
        }
        this.resetStrategy();
    }
    resetStrategy(): void {
        this.recmV = EnumBjlResult.U;
        this.currentMoney = this.option.baseMoney;
        this.redWinStep = this.option.redWinStep;
        this.redFailStep = this.option.redFailStep;
        this.blueWinStep = this.option.blueWinStep;
        this.blueFailStep = this.option.blueFailStep;
        this.betCount = this.option.betCount;
        this.stepMoney = this.option.stepMoney;
        this.recmMoneyArray = [];
        this.betCountArray = [];
        this.runing = true;
    }
    showMoney(): [EnumBjlResult, number] {
        return [this.recmV, this.currentMoney];
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        let recmMoney = this.currentMoney;
        this.recmV = recmV;

        if (recmInfo.summaryInfo.failCount < recmInfo.summaryInfo.winCount) {
            //红色
            if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {

                if (this.redWinStep == 0) {
                    this.resetStrategy();
                    recmMoney = this.currentMoney;
                }
                else if (this.redWinStep > 0) {
                    for (let c = 0; c < this.redWinStep; c++) {
                        this.betCount++;
                        recmMoney = this.currentMoney + (this.betCount * this.stepMoney);
                        this.currentMoney = recmMoney;
                        this.recmMoneyArray.push(recmMoney);
                        this.betCountArray.push(this.betCount);
                    }
                }
                else {

                    let fstep = Math.abs(this.redFailStep);

                    for (let c = 0; c < fstep; c++) {
                        if (this.recmMoneyArray.length > 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.recmMoneyArray[this.recmMoneyArray.length - 1];
                            this.betCount = this.betCountArray[this.betCountArray.length - 1];
                            this.currentMoney = recmMoney;
                        }
                        else if (this.recmMoneyArray.length == 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.option.baseMoney;
                            this.betCount = this.option.betCount;
                            this.currentMoney = recmMoney;
                        }
                        else {
                            recmMoney = this.option.baseMoney;
                            this.currentMoney = recmMoney;
                            this.betCount = this.option.betCount;
                        }
                    }
                }
            }
            else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
                if (this.redFailStep == 0) {
                    this.resetStrategy();
                    recmMoney = this.currentMoney;
                }
                else if (this.redFailStep > 0) {
                    for (let c = 0; c < this.redFailStep; c++) {
                        this.betCount++;
                        recmMoney = this.currentMoney + (this.betCount * this.stepMoney);
                        this.currentMoney = recmMoney;
                        this.recmMoneyArray.push(recmMoney);
                        this.betCountArray.push(this.betCount);
                    }
                }
                else {
                    let fstep = Math.abs(this.redFailStep);

                    for (let c = 0; c < fstep; c++) {
                        if (this.recmMoneyArray.length > 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.recmMoneyArray[this.recmMoneyArray.length - 1];
                            this.betCount = this.betCountArray[this.betCountArray.length - 1];
                            this.currentMoney = recmMoney;
                        }
                        else if (this.recmMoneyArray.length == 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.option.baseMoney;
                            this.betCount = this.option.betCount;
                            this.currentMoney = recmMoney;
                        }
                        else {
                            recmMoney = this.option.baseMoney;
                            this.currentMoney = recmMoney;
                            this.betCount = this.option.betCount;
                        }
                    }
                }
            }
            else { }
        }
        else if (recmInfo.summaryInfo.failCount > recmInfo.summaryInfo.winCount) {
            //蓝色
            if (recmInfo.roundSummaryInfo.lfail >= this.blueStopLFailCount && this.runing) {
                this.runing = false;
                recmInfo.roundSummaryInfo.resetSummaryInfo();
                return [EnumBjlResult.U, 0];
            }
            else if (!this.runing && userRecm.summaryInfo.prevResult == EnumBetResult.Success) {
                this.runing = true;
                if (this.recmMoneyArray.length > 1) {
                    recmMoney = this.recmMoneyArray[this.recmMoneyArray.length - 2];
                    this.betCount = this.betCountArray[this.betCountArray.length - 2];
                    this.currentMoney = recmMoney;
                }
                else {
                    recmMoney = this.option.baseMoney;
                    this.currentMoney = recmMoney;
                    this.betCount = this.option.betCount;
                }
                recmInfo.roundSummaryInfo.resetSummaryInfo();
                this.recmMoneyArray.push(recmMoney);
                this.betCountArray.push(this.betCount);

                return [this.recmV, this.currentMoney];
            }

            if (!this.runing) {
                return [EnumBjlResult.U, 0];
            }
            if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {

                if (this.blueWinStep == 0) {
                    this.resetStrategy();
                    recmMoney = this.currentMoney;
                }
                else if (this.blueWinStep > 0) {
                    for (let c = 0; c < this.blueWinStep; c++) {
                        this.betCount++;
                        recmMoney = this.currentMoney + (this.betCount * this.stepMoney);
                        this.currentMoney = recmMoney;
                        this.recmMoneyArray.push(recmMoney);
                        this.betCountArray.push(this.betCount);
                    }
                }
                else {
                    let wstep = Math.abs(this.blueWinStep);
                    for (let c = 0; c < wstep; c++) {
                        if (this.recmMoneyArray.length > 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.recmMoneyArray[this.recmMoneyArray.length - 1];
                            this.betCount = this.betCountArray[this.betCountArray.length - 1];
                            this.currentMoney = recmMoney;
                        }
                        else if (this.recmMoneyArray.length == 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.option.baseMoney;
                            this.betCount = this.option.betCount;
                            this.currentMoney = recmMoney;
                        }
                        else {
                            recmMoney = this.option.baseMoney;
                            this.currentMoney = recmMoney;
                            this.betCount = this.option.betCount;
                        }
                    }
                }
            }
            else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
                if (this.blueFailStep == 0) {
                    this.resetStrategy();
                    recmMoney = this.currentMoney;
                }
                else if (this.blueFailStep > 0) {
                    for (let c = 0; c < this.blueFailStep; c++) {
                        this.betCount++;
                        recmMoney = this.currentMoney + (this.betCount * this.stepMoney);
                        this.currentMoney = recmMoney;
                        this.recmMoneyArray.push(recmMoney);
                        this.betCountArray.push(this.betCount);
                    }
                }
                else {
                    let fstep = Math.abs(this.blueFailStep);

                    for (let c = 0; c < fstep; c++) {
                        if (this.recmMoneyArray.length > 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.recmMoneyArray[this.recmMoneyArray.length - 1];
                            this.betCount = this.betCountArray[this.betCountArray.length - 1];
                            this.currentMoney = recmMoney;
                        }
                        else if (this.recmMoneyArray.length == 1) {
                            this.recmMoneyArray.pop();
                            this.betCountArray.pop();
                            recmMoney = this.option.baseMoney;
                            this.betCount = this.option.betCount;
                            this.currentMoney = recmMoney;
                        }
                        else {
                            recmMoney = this.option.baseMoney;
                            this.currentMoney = recmMoney;
                            this.betCount = this.option.betCount;
                        }
                    }
                }
            }
            else { }
        }
        else {
            this.resetStrategy();
            recmMoney = this.currentMoney;
        }


        this.recmV = recmV;
        this.currentMoney = recmMoney;

        return [this.recmV, this.currentMoney];
    }
    strategyName: string = '';
    debugCompoenent: string = 'SymmetricMoneyStrategyDebug';
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as SymmetricMoneyStrategy;
        if (obj) {
            //复制
            this.recmV = obj.recmV;
            this.currentMoney = obj.currentMoney;
            this.redWinStep = obj.redWinStep;
            this.redFailStep = obj.redFailStep;
            this.blueWinStep = obj.blueWinStep;
            this.blueFailStep = obj.blueFailStep;
            this.blueStopLFailCount = obj.blueStopLFailCount;
            this.betCount = obj.betCount;
            this.stepMoney = obj.stepMoney;
            this.recmMoneyArray = obj.recmMoneyArray;
            this.betCountArray = obj.betCountArray;
            this.runing = obj.runing;
        }
    }

}